--
-- Author: shunguo.chen
-- Date: 2018-10-05 17:19:49
--
-- LotteryResultView 十连抽结果界面

local Armature = app:getClass("Armature")
local LotteryRender = import(".renders.LotteryRender")
local ResRender = require("app.widget.renders.ResRender")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.LotterResultScene

local NEED_BUY_ITEM_STR = L("mountticket_notenough")

function ClassRef:init()
	self._model = app:getInst("MallModel")
	-- bg fit
	local bg = self._ui["Panel/Panel_1/bg"]
	local size = bg:getContentSize()
	size.width = display.width
	bg:setContentSize(size)

	display.uiAddClick(self._ui.Panel, function()
		if self._showOver then
			executeFunc(self._onCloseView, self)
		end
	end)

	self._ui.Button_1:addClickEventListener(function()
		local isFree = (self._info:getFreeTime() <= 0)
		if isFree then
			executeFunc(self._onOperate, "lottery", 1)
		else
			local have = self._info:getHaveNum()
			local need = self._info.button1Cost:getAmount()
			if need > have then
				local str = string.format(NEED_BUY_ITEM_STR, self._info.button1Cost:getName(true))
				executeFunc(self._onOperate, "needBuy", str)
			else
				executeFunc(self._onOperate, "lottery", 1)
			end
		end
	end)
	self._ui.Button_2:addClickEventListener(function()
		local have = self._info:getHaveNum()
		local need = self._info.button2Cost:getAmount()
		if need > have then
			local str = string.format(NEED_BUY_ITEM_STR, self._info.button2Cost:getName(true))
			executeFunc(self._onOperate, "needBuy", str)
		else
			executeFunc(self._onOperate, "lottery", 2)
		end
	end)

	self._lotteryOnePriceText = self._ui.Button_1:findChild("num_1/num")
	self._lotteryTenPriceText = self._ui.Button_2:findChild("num_2/num")

	self._tenPanel = self._ui["Panel_1/icon_ten"]
	self._threePanel = self._ui["Panel_1/icon_three"]

    self._resRender = ResRender.create(self._ui.Panel_money)
    self._resRender:setData({type = Enums.TipsType.item, id = Constant.Lottery_ItemId, num = 0})
    self._resRender:onClick(function()
    	executeFunc(self._onCloseView, self)
    end)
end

function ClassRef:show()
	if not self._inArmature then
		local params = {
			armatureName = "shaizibengchu",
			path = Res.ChessRollInEfcPath
		}
		local armature = Armature.create(params)
		local posY = self._ui.Panel_1:getPositionY()
		armature:setPosition(display.cx, posY)
		self:addChild(armature, 0)
		self._inArmature = armature
	end

	AM.playSoundById(2392)

	self._inArmature:setVisible(true)
	self._inArmature:play("shaizibengchu", 1, function(sender)
			sender:setVisible(false)
		end)

	self:openAction()
	self:performWithDelay(function()
			self:showNext()
		end, app:getAnimationInterval() * self._openDuration)
end

-- info LotteryInfo
-- datas 奖励
function ClassRef:refresh(info, datas)
	self._ui.Button_1:setVisible(false)
	self._ui.Button_2:setVisible(false)
	self._resRender:setVisible(false)

	self._info = info

	if self._info then
		self._lotteryOnePriceText:setString(info:getFreeTime() <= 0 and info:getFreeTime() or info.button1Cost:getAmount())

		local parent = self._lotteryOnePriceText:getParent()
		local icon = parent:getChildByName("gold")
		display.setNodesMiddle(parent, nil, icon, self._lotteryOnePriceText)

		self._lotteryTenPriceText:setString(info.button2Cost:getAmount())
		local parent = self._lotteryTenPriceText:getParent()
		local icon = parent:getChildByName("gold")
		display.setNodesMiddle(parent, nil, icon, self._lotteryTenPriceText)
	end
	self._datas = datas
	self._dataLen = #datas

	-- init render
	if self._dataLen == 1 and not self._oneRender then
		self._oneRender = LotteryRender.create(self._ui["Panel/Panel_1/icon_one"])
	elseif self._dataLen == 3 and not self._threeRenders then
		local panel = self._threePanel
		local renders = {}
		local render
		for i = 1, 3 do
			renders[#renders+1] = LotteryRender.create(panel:getChildByName("icon_" .. i))
		end
		self._threeRenders = renders
	elseif self._dataLen == 10 and not self._tenRenders then
		local panel = self._tenPanel
		local renders = {}
		local render
		for i=1,10 do
			renders[#renders +1] = LotteryRender.create(panel:getChildByName("icon_" .. i))
		end
		self._tenRenders = renders
	end

	-- init visible
	if self._oneRender then
		self._oneRender:setVisible(false)
	end
	self._tenPanel:setVisible(false)
	self._threePanel:setVisible(false)

	self._renders = nil
	if self._dataLen == 1 then
		self._oneRender:setVisible(true)
		self._renders = {self._oneRender}
	elseif self._dataLen == 3 then
		self._threePanel:setVisible(true)
		self._renders = self._threeRenders
	else
		self._tenPanel:setVisible(true)
		self._renders = self._tenRenders
	end
	for i,render in ipairs(self._renders) do
		render:setOpacity(0)
	end

	self._showOver = false
	self._curIdx = 0
	-- self:showNext()
end

function ClassRef:showNext()
	self._curIdx = self._curIdx + 1
	if self._curIdx <= self._dataLen then
		local render = self._renders[self._curIdx]
		local data = self._datas[self._curIdx]
		render:setData(data)
		render:playAction(function()
			-- 显示特殊获得效果／界面之后才开始下一个
			local mount, breakItem = data.getMount and data:getMount()
			if mount then
				executeFunc(self._onOperate, "lotteryMount", mount, breakItem)
			else
				self:showNext()
			end
		end)
	else
		self._showOver = true
		self._ui.Button_1:setVisible(self._info ~= nil)
		self._ui.Button_2:setVisible(self._info ~= nil)
		self._resRender:setVisible(true)
		self:playAction("word", true)
	end
end

function ClassRef:updateCoin(num)
    self._resRender:setNum(num)
end

function ClassRef:updateMoneyInfo(param)
	if self._resRender then
		local index = self._model:getSelectedIdxById(param.id)
		local moneyInfo = self._model:getShopDrawMoneyInfo(index)
		local conf = GD:getShopDrawInfoById(param.id)
		if moneyInfo and conf then
			self._resRender:setData(moneyInfo)
			local iconPath = string.format("%s%s.png",Res.itemPath,conf.coinIcon)
			self._resRender:setIcon(iconPath)
		end
    end
end

function ClassRef:updateAmazingBoxMoneyInfo(info)
	if self._resRender then
		self._resRender:isNeedSetIcon(true)
		self._resRender:setData(info)
	end
end

return ClassRef